package com.jackrain.nea.core.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.jackrain.nea.core.api.AdLimitValueGroupDelCmd;
import com.jackrain.nea.core.mapper.AdLimitValueGroupMapper;
import com.jackrain.nea.core.mapper.AdLimitValueMapper;
import com.jackrain.nea.core.mapper.MapperShortcut;
import com.jackrain.nea.core.utility.CommandData;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ValueHolder;
import com.jackrain.nea.web.query.QuerySession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "ad")
public class AdLimitValueGroupDelCmdImpl extends CommandAdapter implements AdLimitValueGroupDelCmd {

    @Autowired
    private AdLimitValueGroupMapper adLimitValueGroupMapper;
    @Autowired
    private AdLimitValueMapper adLimitValueMapper;
    @Autowired
    private MapperShortcut mapperShortcut;

    @Override
    @Transactional
    public ValueHolder execute(QuerySession session) throws NDSException {
        CommandData data = new CommandData(session);
        if (data.isDeleting()) {
            if (data.isDeleteMainTable()) {
                adLimitValueGroupMapper.delete(data.getObjId());
                adLimitValueMapper.delete(data.getObjId());
            } else {
                long[] ids = data.getItemsDeleteIds("ad_limitvalue");
                if (ids != null && ids.length > 0) {
                    mapperShortcut.delete(ids, "ad_limitvalue");
                }
            }
            return data.success();
        }
        throw data.notSupported();
    }
}
